package com.pojoin.cms.authentication;

/**
 * 通用权限法则
 * @author pojoin
 *
 */
public class GenericPrivilege {
	public final static int NO_PRIVILEGE = 0;
	public final static int QUERY_OR_USE_PRIVILEGE = 1;// 查看权限
	public final static int CREATE_PRIVILEGE = 2;// 添加权限
	public final static int DELETE_PRIVILEGE = 4;// 删除权限
	public final static int UPDATE_PRIVILEGE = 8;// 更新权限
	public final static int ALL_PRIVILEGE = QUERY_OR_USE_PRIVILEGE
			| CREATE_PRIVILEGE | DELETE_PRIVILEGE | UPDATE_PRIVILEGE;// 增删改查权限

	/**
	 * 判断是否具有权限
	 * @param privilege 权限值
	 * @return
	 */
	public static boolean isValidPrivilege(int privilege)
	{
		if ((privilege & QUERY_OR_USE_PRIVILEGE) != 0) {
			return true;
		}

		if ((privilege & CREATE_PRIVILEGE) != 0) {
			return true;
		}

		if ((privilege & DELETE_PRIVILEGE) != 0) {
			return true;
		}

		if ((privilege & UPDATE_PRIVILEGE) != 0) {
			return true;
		}

		return false;
	}

	/**
	 * 判断是否具有察看权限
	 * @param privilege
	 * @return
	 */
	public static boolean checkQueryPrivilege(int privilege)
	{
		if ((privilege & QUERY_OR_USE_PRIVILEGE) != 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否有添加权限
	 * @param privilege
	 * @return
	 */
	public static boolean checkCreatePrivilege(int privilege)
	{
		if ((privilege & CREATE_PRIVILEGE) != 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否有删除权限
	 * @param privilege
	 * @return
	 */
	public static boolean checkDeletePrivilege(int privilege)
	{
		if ((privilege & DELETE_PRIVILEGE) != 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 判断是否有修改权限
	 * @param privilege
	 * @return
	 */
	public static boolean checkUpdatePrivilege(int privilege) {
		if ((privilege & UPDATE_PRIVILEGE) != 0) {
			return true;
		} else {
			return false;
		}
	}

}
